﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using FPP.Models;
using FPP.Models.Collections;

namespace FPP.Controllers
{
    public partial class GamesController
    {
        
        public JsonResult TenGames()
        {
            var jGames = new List<jsonGame>();
            var selectDate = DateTime.Now.AddMonths(-2);

            IEnumerable<PoolType> lGameTypes;
            using (context)
            {
                lGameTypes = context.PoolTypes.ToList().Where(g => DateTime.Parse(g.DateEnd) > selectDate);
            }

            foreach (PoolType g in lGameTypes)
            {
                //Start the object and add it to the collection
                var jg = new jsonGame();
                jGames.Add(jg);

                //Fill the object
                jg.Name = g.Name;
                jg.DetailsURL = string.Format("/Games/Details/{0}", g.Id);
                jg.JoinURL = string.Format("/Roster/Index/{0}", g.Id);
                jg.StartDate = g.DateStart;
                jg.EndDate = g.DateEnd;
            }

            return Json( jGames
                , JsonRequestBehavior.AllowGet);
        }

        public JsonResult UsersInGame(int gameId)
        {
            var oUsers = new List<User>();

            using (context)
            {
                var q = from u in context.Users 
                        join p in context.Pools on u.Id equals p.UserId
                        where p.PoolTypeId == gameId
                        select u;

                foreach (User u in q)
                {
                   if (!oUsers.Contains(u))
                       oUsers.Add(u);
                }

            }
            return Json(oUsers, JsonRequestBehavior.AllowGet);
        }

    }
}
